/**
 * Prototype enabled adaptation of my old linklistener principle (example can be found at http://www.windgazer.nl/projects/jsrepository/)
 * Comes built-in with the 'autosize' and 'external' handlers.
 * 'autosize' attempts to open a window and resize it to fit the contents, without screen-clutter but with scrollbars (you never know, something might not fit...).
 * 'external' simply opens up a new window.
 *  
 * @class
 * @version 1.0.070721
 * @author Martin Reurings - http://www.windgazer.nl
 * @requires Prototype
 * @see Prototype
 * @depends prototype.js
 */
var LinkListener = {
	handlers: {
		external:function(link) {
			var options = "";
			return this.popup(link, options);
		},
		popup:function(link, options) {
			var options = options||"resizable=yes,toolbar=no,statusbar=no,scrollbars=yes";
			var targetWindow = window.open(link.href, "_blank", options);
			return true;
		},
		/**
		 * Allows the popup-window to be controlled using classname-parameters.
		 * The 'width', 'height' and 'target' can be controlled using a
		 * parameter:value pair, for example:<br />
		 * &lt;a href="detail.html" rel="advancedpopup" class="width:200 height:300 target:detail">my details&lt;/a><br />
		 * If dimensions are required, both must be set! Setting only a single dimension
		 * width, or height, gets ignored!
		 */
		advancedpopup:function(link) {
			var properties = getClassNameProperties(link.className);
			var target = properties.target||"_blank";
			Element.extend(target);
			var options = "resizable=yes,toolbar=no,statusbar=no,scrollbars=yes,width=200,height=200";
			if (properties.width && properties.height) {
				var targetWindow=window.open("about:blank", target, options);
				var width = parseInt(properties.width||300, 10);
				var height = parseInt(properties.height||300, 10);
				targetWindow.resizeTo(width, height);
				targetWindow.document.write("<html><head><style>html,body{margin:0;padding:0;height:100%;width:100%}</style></head><body></body></html>");
				var dummy = targetWindow.document.body;
				var actualWidth = dummy.clientWidth;
				var actualHeight = dummy.clientHeight;
				var correctedWidth = width + (width - actualWidth);
				var correctedHeight = height + (height - actualHeight);
				targetWindow.resizeTo(correctedWidth, correctedHeight);
				targetWindow.document.location = link.href;
			} else {
				var targetWindow=window.open(link.href, target, options);
			}
			targetWindow.focus();//In case of pre-existing window
			return true;//Stop original link
		},
		external:function(link) {
			window.open(link.href, "_blank");
			return true;//Stop original link
		}
	},
	click:function(e) {
		if (e.button > 1 || e.shiftKey || e.ctrlKey || e.altKey || e.metaKey) return false;//Check for primary button and/or modifiers, always bail out if it isn't a clean primary click...
		var link = Event.element(e);
		if (!link.nodeName.match(/^a$/i)) link = e.findElement("a");
		if (link && link.rel && this.handlers[link.rel] && this.handlers[link.rel](link)) {
			return e.stop();
		}
		return false;
	},
	/**
	 * Add an extra handler to the listener.
	 * Use this method to add an extra handler for
	 * a rel that hasn't been defined by default.
	 * 
	 * @param {String} id The value of the rel-attribute on which you want to react.
	 * @param {function} handler A function to handle the event, takes the link as a parameter.
	 */
	addHandler:function(id, handler) {
		this.handlers[id] = handler;
	},
	init:function () {
		//Event.observe was unreliable during 'runtime' so for initialisation I'm falling back on my own tools...
		Events.attach(document.documentElement||document.body, "click", function(e){
			var e = e||event;
			Event.extend(e);
			LinkListener.click(e);
		});
	}
}

/**
 * A function to split up an elements className into property pairs when
 * defined as 'property:value'-pairs within the className.
 */
function getClassNameProperties(className) {
	var classNames = className.split(" ");
	var properties = {
		className: className
	}
	for (var i = 0; i < classNames.length; i++) {
		var cn = classNames[i];
		if (cn.indexOf(":") > 1) {
			var cns = cn.split(":");
			properties[cns[0]] = cns[1];
		}
	}
	return properties;
}

LinkListener.init();
